// pages/particulars/particulars.js
var {commodityxq,addcar} = require('../../utils/util');
Page({
  onShareAppMessage() {
    return {
      title: 'swiper',
      path: 'page/component/pages/swiper/swiper'
    }
  },

   /**
    * 页面的初始数据
    */
   data: {
    userInfo: {},
    skum:[],
    head_floot_image:'',
    head_floot_price:'',
    type:[
      // '深空灰色','银色'
   ],
   content:[
      '256GB 固态硬盘','8GB 统一内存'
   ],
    url:'http://img.9lele.com',
    date:"",
    sum:0,
    background: [],
    background1: [],
    title:'',
    indicatorDots: false,
    vertical: false,
    autoplay: true,
    interval: 10000,
    duration: 500,
    // sku
    pid:0,
    price:0,
    pid_1:[],
    pid_2:[],
    son:[],
    sonpid:0,
    sku:[],
    skupid:0,
    skupid0:[],
    skuson:[],
    skusonpid:0,
    // 加入购物车数据
    typecontent:'',
    typecolor:'',
    image:"",
    price_3:'',
    // 商品名称
    title_name:[]

   },
  //  颜色分类
   type(e){
     console.log(e);
    let skuson=[]
    for(let i=0;i< this.data.sku.length;i++){
      if(this.data.sku[i].pid == e.currentTarget.dataset.id.id){
        skuson.push(this.data.sku[i])
      }
    }
    this.setData({
      skuson:skuson,
      price:skuson[0].price,
      skusonpid:skuson[0].id,
      skupid:e.currentTarget.dataset.id.id,
      typecolor:e.currentTarget.dataset.id.name
    })
      
      
   },
  //  磁盘容量
   content(e){
    
    // console.log(e.currentTarget.dataset.id.name);
    this.setData({
      price:e.currentTarget.dataset.id.price,
      skusonpid:e.currentTarget.dataset.id.id,
      typecontent:e.currentTarget.dataset.id.name
    })
    // console.log(this.data.typecontent);
   },
   changeIndicatorDots() {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },

  changeAutoplay() {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },

  intervalChange(e) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange(e) {
    this.setData({
      duration: e.detail.value
    })
  },
   home(){
    wx.navigateBack({
      delta: 1
    })
   },
  //  点击立即购买
  shopping_1(){
    this.showModal();
    this.get()
  },
  // 获取去授权
  get(){
    wx.getStorage({
      key: 'u1',
      success (res) {
        // console.log(res.data)
        // let arr = JSON.price(res.data)
      },
      fail(){
        wx.switchTab({
          url: '/pages/login/login',
        })
      }
    })
  },
  //显示对话框
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
   //隐藏对话框
   hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },

   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function (options) {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
    //  console.log(options.id);
     let id =options.id
   
    let that = this
    wx.request({
      url: commodityxq(id),
      success (res) {

        that.setData({
          title_name:res.data.data,
          background:res.data.data.imgList,
          price:res.data.data.market_price,
          title:res.data.data.goods_name,
          head_floot_image:res.data.data.original_img,
          head_floot_price:res.data.data.market_price,
        })
        that.setData({
            sum:that.data.background.length
        })
        // console.log(that.data.type);
        console.log(res.data.data.sku);
                var s = "";
                if(res.data.data.goods_content.length == 0) return "";
                s = res.data.data.goods_content.replace(/&amp;/g,"&");
                s = s.replace(/&lt;/g,"<");
                s = s.replace(/&gt;/g,">");
                s = s.replace(/&nbsp;/g," ");
                s = s.replace(/&#39;/g,"\'");
                s = s.replace(/&quot;/g,"\"");
                s = s.replace(/\<img/g,"<img style='width:100%' ")
              //  console.log(s);
               that.setData({
                 date:s,
                 image:res.data.data.original_img
               })  
               console.log(res.data.data);
               let price=res.data.data.shop_price
        if(res.data.data.sku != ''){
          let sku0=[]
          for(let i=0;i < res.data.data.sku.length;i++){
            if(res.data.data.sku[i].pid == 0){
              sku0.push(res.data.data.sku[i]);
            }
          }
          let skuson=[]
          for(let i=0;i< res.data.data.sku.length;i++){
            if(res.data.data.sku[i].pid == sku0[0].id){
              skuson.push(res.data.data.sku[i])
            }
          }
          price = skuson[0].price
          that.setData({
            sku:res.data.data.sku,
            skupid:sku0[0].id,
            skupid0:sku0,
            skuson:skuson,
            skusonpid:skuson[0].id
          })
        }
        that.setData({
          price:price
        })
        
      }
      
    })
   },

   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady: function () {

   },

   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function () {
    wx.getStorage({
      key: 'openid',
      success (res) {
       
        let arr = JSON.parse(res.data)
        console.log(arr)
      }
    })
   }, 

   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function () {

   },

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function () {

   },

   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function () {

   },

   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom: function () {

   },

   /**
    * 用户点击右上角分享
    */
   onShareAppMessage: function () {

   },
   bk(e){
    //  .
    // let arr = e.target.dataset.id
    // let arr2 = this.data.background;
    // arr2[arr] = "/static/head/41FPFHZ$7FPY7YRK0VW[}QM.png"
    // this.setData({
    //   background1:arr2
    // }
  },
  // 弹窗确认购买加入购物车
  addshoping(){
    let userInfo = this.data.userInfo
    console.log(userInfo);
    wx.login({
      success (res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: 'https://example.com/onLogin',
            data: {
              code: res.code
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },
 
  onShareAppMessage() {
    let that = this

    wx.requestPayment({
      timeStamp: '',
      nonceStr: '',
      package: '',
      signType: 'MD5',
      paySign: '',
      success (res) { },
      fail (res) { }
    })
    // return {
    //   title: that.data.title,
    //   path: '/pages/particulars/particulars',
    //   imageUrl:that.data.url+that.data.background[0].image_url
    //   // promise 
    // }
  },
  // 分享朋友圈
  onShareTimeline(){
    let that = this
    
    // return {
    //   title: that.data.title,
    //   query: '/pages/particulars/particulars',
    //   imageUrl:that.data.url+that.data.background[0].image_url
    //   // promise 
    // }
  },
    // 点击确定按钮添加购物车
  addshoping(e){
    let that = this
    console.log(e.currentTarget.dataset.id);
   let content = that.data.title_name
   console.log(content);
   wx.getStorage({
    key: 'openid',
    success (res) {
      // console.log(res.data)
      let arr = JSON.parse(res.data)
      console.log(arr)
        wx.request({
        url:addcar(), //仅为示例，并非真实的接口地址
        method:'POST',
        data: {
          goods_id:content.goods_id,
          goods_name:content.goods_name,
          price:content.shop_price,
          number:1,
          goods_img:content.original_img,
          member_id:arr.member_id
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success (res) {
          wx.showToast({
            title: '添加成功',
            icon: 'success',
            duration: 2000,
            success(){
              setTimeout(function(){
                  wx.switchTab({
                    url: '/pages/shopping/shopping',
                  })
              },2000)
            }
          })
        }
      })
    }
  })
    
  }
})